package blancosMikel.model.config;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.sql.DataSource;

public class serviceLocator {
	
	private static serviceLocator instance=null;
	private InitialContext mycontext;
	private String strDataSource;
	private DataSource dataSource;	
	/**
	 * Contructor de Clase
	 * pass svn yA9hg4aP5pD3
	 */
	public serviceLocator() {
		
		try {
			contextConnection();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}
	private void contextConnection() throws NameNotFoundException{
		try {
			mycontext= new InitialContext();
			strDataSource="java:comp/env/jdbc/sigaabm";
			dataSource=(DataSource) mycontext.lookup(strDataSource);
			System.out.println(dataSource);
			System.out.println("Levantanto servicio MYSQL");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	/**
	 * implementacion del patron singleton par que esta clase solo 
	 * exista una sola vez en todo el proyecto.
	 * @return
	 */
	public static serviceLocator getInstance(){
		if(instance==null){
			synchronized (serviceLocator.class) {
				if(instance==null){
					instance= new serviceLocator();
				}
			} 
		}
		return instance;
	}	
	public Connection getMYSQLConnection() throws SQLException{
		return this.dataSource.getConnection();
	}
}
